package com.face.service;

import com.face.dao.ShopDao;
import com.face.entity.Account;
import com.face.entity.Shop;
import com.face.entity.Visit;
import com.face.exception.DatabaseException;
import com.face.exception.NoShopException;
import com.face.vo.ShopInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;

@Service
public class ShopService {
	
	@Autowired
	private ShopDao shopDao;
	@Autowired
	private AccountService accountService;
	
	
	
	public Shop getShop(String shopId,String deviceId){
		Shop shop=shopDao.getShop(shopId, deviceId);
		if(shop==null){
			throw new NoShopException("没有此店面");
		}
		return shop;
	}

	public  Integer saveVisit(Visit visit){

		Integer id = shopDao.saveVisit(visit);
		if (id < 0) {
			throw new DatabaseException("visit", "save", "visit");
		}

		return id;

	}

	public void save(Shop shop) {
		shopDao.save(shop);
	}

    public Shop findByShopId(String shopId) {
		return shopDao.findByShopId(shopId);
    }

	public void update(Shop shop) {
		shop.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		shopDao.update(shop);
	}

    public Shop findShopByActiveCode(String activeCode) {
		return shopDao.findShopByActiveCode(activeCode);
    }

    public ShopInfoVO findShopInfoById(String shopId) {
		ShopInfoVO shopInfoVO = new ShopInfoVO();
		Shop shop = shopDao.findByShopId(shopId);
		BeanUtils.copyProperties(shop, shopInfoVO);
		Account account = accountService.findManagerByShopId(shopId);
		shopInfoVO.setEmail(account.getEmail());
		shopInfoVO.setUsername(account.getUsername());
		shopInfoVO.setPassword(account.getPassword());
		return shopInfoVO;
    }
}
